package com.trailwalker.traceability;

import com.alibaba.fastjson.JSON;
import com.trailwalker.JavaUtils.file.FileUtils;
import com.trailwalker.JavaUtils.log.LogUtils;
import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.core.activity.Activity;
import com.trailwalker.javafx.core.application.JavaFxApplication;
import com.trailwalker.javafx.core.entity.Bundle;
import com.trailwalker.javafx.core.entity.Intent;
import com.trailwalker.javafx.utils.resource.ResourceUtils;
import com.trailwalker.javafx.utils.stage.StageUtils;
import com.trailwalker.javafx.utils.storage.PreferencesManager;
import com.trailwalker.traceability.activity.*;
import com.trailwalker.traceability.entitiy.Config;
import com.trailwalker.traceability.entitiy.application.GlobalData;
import com.trailwalker.traceability.entitiy.enum_entity.ProcessType;
import com.trailwalker.traceability.service.ApiDataService;
import javafx.scene.Scene;
import javafx.scene.image.Image;

import java.io.File;

/**
 * @author Trailwalker
 * @create 2021-11-11 10:28
 */
public class TraceabilitySystemApplication extends JavaFxApplication {

    private static final String TAG = TraceabilitySystemApplication.class.getSimpleName();

    private GlobalData mGlobalData;
    private Config mConfig;

    public GlobalData getGlobalData() {
        return mGlobalData;
    }

    public Config getConfig() {
        return mConfig;
    }

    public void reload() {
        initLoad();
        super.reload();
    }

    public Class<? extends Activity> getActivityClass(ProcessType processType) {
        Class<? extends Activity> activityClass = null;
        switch (processType) {
            case SEMI_FINISHED_PRODUCT_TEST:
                activityClass = SemiFinishedProductTestActivity.class;
                break;
            case SINGLE_BOARD_TEST:
                activityClass = SingleBoardTestActivity.class;
                break;
            case DISPENSING_STATION_TEST:
                activityClass = DispensingStationTestActivity.class;
                break;
            case FINISHED_PRODUCT_TEST:
                activityClass = FinishedProductTestActivity.class;
                break;
            case LABORATORY:
                activityClass = LaboratoryActivity.class;
                break;
            case FINAL_INSPECTION:
                activityClass = FinalInspectionActivity.class;
                break;
            case PACKAGING:
                activityClass = PackagingActivity.class;
                break;
            case PUT_IN_STORAGE:
                activityClass = PutInStorageActivity.class;
                break;
            case OUTBOUND:
                activityClass = OutBoundActivity.class;
                break;
            default:
                break;
        }
        return activityClass;
    }

    /**
     * main线程
     *
     * @param args
     */
    public static void main(String[] args) {
        LogUtils.log(TAG, "application main work at Thread:" + Thread.currentThread().getName());
        launch(args);
    }

    @Override
    public void onApplicationInit() {
        initLoad();
    }

    private void initLoad() {
        if (mGlobalData == null) {
            mGlobalData = new GlobalData();
        } else {
            mGlobalData.clear();
        }
        File configFile = FileUtils.getFile(System.getProperty("user.dir") + "/config/config.json");
        if (configFile.exists()) {
            String configJsonStr = FileUtils.readJsonFile(configFile.getAbsolutePath());
            if (StringUtils.isValid(configJsonStr)) {
                mConfig = JSON.parseObject(configJsonStr, Config.class);
                if (mConfig != null) {
                    String ip = mConfig.getIp();
                    if (StringUtils.isValid(ip)) {
                        mGlobalData.putString("ip", ip);
                    }
                }
            }
        }

    }

    @Override
    public void onApplicationStart() {
        StageUtils.setTitle(getStage(), "益华扫码流程追溯系统");
        StageUtils.addIcon(getStage(), new Image(ResourceUtils.getImage(getApplication(), "logo.jpg")));
        Scene scene = getScene();
        scene.getStylesheets().add(ResourceUtils.getCss(this, "lightTheme.css"));
    }

    @Override
    public void onApplicationStop() {
        ApiDataService.getInstance().release();
    }

    @Override
    public Intent initMainActivity() {
        PreferencesManager preferencesManager = PreferencesManager.getInstance();
        String account = preferencesManager.getString("account", "");
        String password = preferencesManager.getString("password", "");
        Intent intent = new Intent(LoginActivity.class);
        if (StringUtils.isValid(account) && StringUtils.isValid(password)) {
            Bundle bundle = new Bundle();
            bundle.putBoolean("isAutomaticLogin", true);
            bundle.putString("account", account);
            bundle.putString("password", password);
            intent.setBundle(bundle);
        }
        return intent;
    }
}
